package com.fyb.middleware.value.impl;

import com.alibaba.fastjson.JSON;
import com.fyb.middleware.Constants;
import com.fyb.middleware.annotation.DoRateLimiter;
import com.fyb.middleware.value.IValveService;
import com.google.common.util.concurrent.RateLimiter;
import org.aspectj.lang.ProceedingJoinPoint;

import java.lang.reflect.Method;

/**
 * @Author：MR.FANG
 * @name：RateLimiterValue
 * @Date：2025/6/10 下午3:47
 */
public class RateLimiterValue implements IValveService {

    @Override
    public Object access(ProceedingJoinPoint jp, Method method, DoRateLimiter doRateLimiter, Object[] args) throws Throwable {
        //判断是否开启
        if(0 == doRateLimiter.permitsPerSecond()) {
            return jp.proceed();
        }

        String clazzName = jp.getTarget().getClass().getName();
        String methodName = method.getName();

        String key = clazzName + "." + methodName;

        if (null == Constants.rateLimiterMap.get(key)){
            Constants.rateLimiterMap.put(key, RateLimiter.create(doRateLimiter.permitsPerSecond()));
        }

        RateLimiter rateLimiter = Constants.rateLimiterMap.get(key);
        if (rateLimiter.tryAcquire()){
            return jp.proceed();
        }

        return JSON.parseObject(doRateLimiter.returnJson(), method.getReturnType());
    }
}
